from os import path
from rest_framework import serializers
from api.attachment_management.serializers import AlloyPhaseDiagramSerializer

from .models import ChemicalProperties,\
    PhysicalProperties,\
    TensileMechanicalProperties,\
    CompressingMechanicalProperties,\
    ShearProperties,\
    BondProperties,\
    TorsionalProperties,\
    FractureToughness,\
    FrictionalWearProperties,\
    Hardness,\
    Sample,\
    MacroOrganization,\
    MicroStructure,\
    ServiceProperties


# 文件上传根目录
MEDIA_ROOT = 'C:/mat_gene_attachments/'


class ChemicalPropertiesSerializer(serializers.HyperlinkedModelSerializer):
    """
    化学性能序列化器
    """

    readable_alloy_code = serializers.SerializerMethodField()
    polarization_curve_filename = serializers.SerializerMethodField()
    open_circuit_potential_curve_filename = serializers.SerializerMethodField()
    tg_curve_filename = serializers.SerializerMethodField()

    class Meta:
        model = ChemicalProperties
        fields = (
            'url', 'sample_name', 'alloy_code', 'readable_alloy_code', 'corrosion_potential',
            'corrosion_current', 'erosion', 'passivation_current', 'polarization_curve_filename',
            'polarization_curve', 'open_circuit_potential', 'open_circuit_potential_curve_filename',
            'open_circuit_potential_curve', 'corrosion_rate', 'tg_curve_filename', 'tg_curve', 'toxicity'
        )

    def get_readable_alloy_code(self, obj):
        return obj.alloy_code.alloy_code

    def get_polarization_curve_filename(self, obj):
        # 若没有附件，则返回''，否则返回文件名
        filename = obj.polarization_curve.name
        return '' if filename is None else path.basename(filename)

    def get_open_circuit_potential_curve_filename(self, obj):
        # 若没有附件，则返回''，否则返回文件名
        filename = obj.open_circuit_potential_curve.name
        return '' if filename is None else path.basename(filename)

    def get_tg_curve_filename(self, obj):
        # 若没有附件，则返回''，否则返回文件名
        filename = obj.tg_curve.name
        return '' if filename is None else path.basename(filename)


class PhysicalPropertiesSerializer(serializers.HyperlinkedModelSerializer):
    """
    物理性能序列化器
    """
    impedance_filename = serializers.SerializerMethodField()
    impedance_curve_filename = serializers.SerializerMethodField()
    readable_alloy_code = serializers.SerializerMethodField()
    class Meta:
        model = PhysicalProperties
        fields = (
            'url', 'sample_name', 'alloy_code', 'density', 'melting_point', 'boiling_point',
            'sample_density', 'conductivity', 'resistivity', 'thermal_conductivity',
            'thermal_expansion_coefficient', 'permeability', 'magnetic_saturation', 'remanence',
            'coercive_force', 'liquid_temperature', 'solidus_temperature', 'melting_process',
            'enthalpy', 'supercooling_degree', 'thermal_diffusion_coefficient', 'specific_heat_capacity',
            'wetting_force', 'wetting_rate', 'wetting_time', 'spreading_rate', 'spreading_square',
            'wetting_angle', 'molar_heat_capacity', 'impedance', 'impedance_curve', 'radioactive',
            'impedance_filename', 'impedance_curve_filename', 'readable_alloy_code'
        )

    def get_impedance_filename(self, obj):
        filename = obj.impedance.name
        return '' if filename is None else path.basename(filename)

    def get_impedance_curve_filename(self, obj):
        filename = obj.impedance_curve.name
        return '' if filename is None else path.basename(filename)

    def get_readable_alloy_code(self, obj):
        return obj.alloy_code.alloy_code

class TensileMechanicalPropertiesSerializer(serializers.HyperlinkedModelSerializer):
    """
    拉伸力学性能序列化器
    """
    readable_alloy_code = serializers.SerializerMethodField()
    engineering_stress_strain_filename = serializers.SerializerMethodField()
    true_stress_strain_filename = serializers.SerializerMethodField()

    class Meta:
        model = TensileMechanicalProperties
        fields = (
            'url', 'sample_name', 'alloy_code', 'readable_alloy_code', 'data_source', 'sample_status',
            'equipment_name', 'equipment_manufacturer', 'device_model', 'load',
            'stretching_rate', 'strain_rate', 'sample_range', 'sample_thickness',
            'sample_width', 'test_temperature', 'young_modulus', 'yield_strength',
            'up_yield_strength', 'down_yield_strength', 'ultimate_tensile_strength',
            'fracture_strength', 'break_elongation', 'section_reduction', 'uniform_elongation',
            'true_stress', 'true_strain', 'work_hardening_rate', 'engineering_stress_strain',
            'true_stress_strain','engineering_stress_strain_filename','true_stress_strain_filename'
        )

    def get_readable_alloy_code(self, obj):
        return obj.alloy_code.alloy_code

    def get_engineering_stress_strain_filename(self, obj):
        # 若没有附件，则返回''，否则返回文件名
        filename = obj.engineering_stress_strain.name
        return '' if filename is None else path.basename(filename)

    def get_true_stress_strain_filename(self, obj):
        # 若没有附件，则返回''，否则返回文件名
        filename = obj.true_stress_strain.name
        return '' if filename is None else path.basename(filename)


class CompressingMechanicalPropertiesSerializer(serializers.HyperlinkedModelSerializer):
    """
    压缩力学性能序列化器
    """

    readable_alloy_code = serializers.SerializerMethodField()
    compress_stress_strain_filename = serializers.SerializerMethodField()

    class Meta:
        model = CompressingMechanicalProperties
        fields = (
            'url', 'sample_name', 'alloy_code', 'readable_alloy_code', 'data_source', 'sample_status',
            'equipment_name', 'equipment_manufacturer', 'device_model', 'compress_rate',
            'sample_height', 'sample_diameter', 'sample_length', 'sample_width',
            'test_temperature', 'compression_modulus', 'yield_strength', 'ultimate_compress_strength',
            'fracture_strength', 'break_elongation', 'compress_stress_strain', 'compress_stress_strain_filename',
        )

    def get_readable_alloy_code(self, obj):
        return obj.alloy_code.alloy_code

    def get_compress_stress_strain_filename(self, obj):
        # 若没有附件，则返回''，否则返回文件名
        filename = obj.compress_stress_strain.name
        return '' if filename is None else path.basename(filename)


class ShearPropertiesSerializer(serializers.HyperlinkedModelSerializer):
    """
    剪切性能序列化器
    """
    readable_alloy_code = serializers.SerializerMethodField()

    class Meta:
        model = ShearProperties
        fields = (
            'url', 'sample_name', 'alloy_code', 'data_source', 'sample_status',
            'equipment_name', 'equipment_manufacturer', 'device_model', 'temperature',
            'cross_sectional_area', 'shear_load', 'shear_modules', 'ultimate_shear_strength',
            'shear_yield_strength',
            'readable_alloy_code'
        )
    def get_readable_alloy_code(self, obj):
        return obj.alloy_code.alloy_code


class BondPropertiesSerializer(serializers.HyperlinkedModelSerializer):
    """
    弯曲性能序列化器
    """

    readable_alloy_code = serializers.SerializerMethodField()

    class Meta:
        model = BondProperties
        fields = (
            'url', 'sample_name', 'alloy_code', 'readable_alloy_code', 'data_source', 'sample_status',
            'equipment_name', 'equipment_manufacturer', 'device_model', 'temperature',
            'sample_range', 'sample_width', 'sample_height', 'flexural_modulus',
            'bond_strength'
        )

    def get_readable_alloy_code(self, obj):
        return obj.alloy_code.alloy_code


class TorsionalPropertiesSerializer(serializers.HyperlinkedModelSerializer):
    """
    扭转性能序列化器
    """

    readable_alloy_code = serializers.SerializerMethodField()

    class Meta:
        model = TorsionalProperties
        fields = (
            'url', 'sample_name', 'alloy_code', 'readable_alloy_code', 'data_source', 'sample_status',
            'equipment_name', 'equipment_manufacturer', 'device_model', 'temperature',
            'sample_range', 'sample_diameter', 'torsional_modules', 'torsional_strength',
            'torque', 'torsional_angle'
        )

    def get_readable_alloy_code(self, obj):
        return obj.alloy_code.alloy_code


class FractureToughnessSerializer(serializers.HyperlinkedModelSerializer):
    """
    断裂韧性序列化器
    """

    readable_alloy_code = serializers.SerializerMethodField()

    class Meta:
        model = FractureToughness
        fields = (
            'url', 'sample_name', 'alloy_code', 'readable_alloy_code', 'data_source', 'sample_status',
            'equipment_name', 'equipment_manufacturer', 'device_model', 'temperature',
            'sample_range', 'sample_width', 'sample_thickness', 'open_depth',
            'open_shape', 'fracture_toughness'
        )

    def get_readable_alloy_code(self, obj):
        return obj.alloy_code.alloy_code


class FrictionalWearPropertiesSerializer(serializers.HyperlinkedModelSerializer):
    """
    摩擦磨损性能序列化器
    """

    readable_alloy_code = serializers.SerializerMethodField()

    class Meta:
        model = FrictionalWearProperties
        fields = (
            'url', 'sample_name', 'alloy_code', 'readable_alloy_code', 'data_source', 'sample_status',
            'equipment_name', 'equipment_manufacturer', 'device_model', 'sample_shape',
            'sample_thickness', 'sample_diameter', 'test_temperature', 'sample_quality_before_test',
            'sample_volume_before_test', 'friction_test', 'test_time', 'applied_load', 'friction_velocity',
            'friction_distance', 'friction_coefficient', 'mass_wear', 'volume_wear', 'mass_wear_rate',
            'volume_wear_rate'
        )

    def get_readable_alloy_code(self, obj):
        return obj.alloy_code.alloy_code


class HardnessSerializer(serializers.HyperlinkedModelSerializer):
    """
    硬度序列化器
    """

    readable_alloy_code = serializers.SerializerMethodField()

    class Meta:
        model = Hardness
        fields = (
            'url', 'sample_name', 'alloy_code', 'readable_alloy_code', 'data_source', 'sample_status',
            'equipment_name', 'equipment_manufacturer', 'device_model', 'sample_shape',
            'test_temperature', 'test_load', 'hardness'
        )

    def get_readable_alloy_code(self, obj):
        return obj.alloy_code.alloy_code


class SampleSerializer(serializers.HyperlinkedModelSerializer):
    """
    样本管理序列器
    """

    readable_subject_name = serializers.SerializerMethodField()
    readable_experiment_name = serializers.SerializerMethodField()
    readable_alloy_code = serializers.SerializerMethodField()
    data_source_attachment_filename = serializers.SerializerMethodField()

    class Meta:
        model = Sample
        fields = (
            'url', 'subject_name', 'experiment_name', 'alloy_code', 'sample_name',
            'english_name', 'alias', 'production_unit', 'application_overview',
            'data_source', 'data_source_attachment',
            'readable_subject_name', 'readable_experiment_name', 'readable_alloy_code',
            'data_source_attachment_filename'
        )


    def get_readable_subject_name(self, obj):
        return obj.subject_name.subject_name

    def get_readable_experiment_name(self, obj):
        return obj.experiment_name.experiment_name

    def get_readable_alloy_code(self, obj):
        return obj.alloy_code.alloy_code

    def get_data_source_attachment_filename(self, obj):
        # 若没有附件，则返回''，否则返回文件名
        filename = obj.data_source_attachment.name
        return '' if filename is None else path.basename(filename)


class MacroOrganizationSerializer(serializers.HyperlinkedModelSerializer):
    """
    宏观形貌管理序列器
    """
    readable_alloy_code = serializers.SerializerMethodField()

    class Meta:
        model = MacroOrganization
        fields = (
            'url', 'sample_name', 'alloy_code', 'sample_form', 'sample_shape',
            'sample_length', 'sample_width', 'sample_thickness', 'sample_diameter',
            'sample_col', 'sample_gloss', 'sample_roughness',
            'readable_alloy_code'
        )

    def get_readable_alloy_code(self, obj):
        return obj.alloy_code.alloy_code


class MicroStructureSerializer(serializers.HyperlinkedModelSerializer):
    """
    微观形貌管理序列器
    """
    readable_alloy_code = serializers.SerializerMethodField()
    second_phase_filename = serializers.SerializerMethodField()
    grain_morphology_filename = serializers.SerializerMethodField()
    fracture_morphology_filename = serializers.SerializerMethodField()
    corrosion_morphology_filename = serializers.SerializerMethodField()
    oxidation_morphology_filename = serializers.SerializerMethodField()

    class Meta:
        model = MicroStructure
        fields = (
            'url', 'sample_name', 'alloy_code', 'organization_structure', 'second_phase',
            'grain_morphology', 'average_grain_size', 'second_phase_size', 'fracture_type',
            'fracture_morphology', 'corrosion_morphology', 'oxidation_morphology',
            'readable_alloy_code',
            'second_phase_filename', 'grain_morphology_filename',
            'fracture_morphology_filename', 'corrosion_morphology_filename',
            'oxidation_morphology_filename'
        )

    def get_readable_alloy_code(self, obj):
        return obj.alloy_code.alloy_code

    def get_second_phase_filename(self, obj):
        # 若没有附件，则返回''，否则返回文件名
        filename = obj.second_phase.name
        return '' if filename is None else path.basename(filename)

    def get_grain_morphology_filename(self, obj):
        # 若没有附件，则返回''，否则返回文件名
        filename = obj.grain_morphology.name
        return '' if filename is None else path.basename(filename)

    def get_fracture_morphology_filename(self, obj):
        # 若没有附件，则返回''，否则返回文件名
        filename = obj.fracture_morphology.name
        return '' if filename is None else path.basename(filename)

    def get_corrosion_morphology_filename(self, obj):
        # 若没有附件，则返回''，否则返回文件名
        filename = obj.corrosion_morphology.name
        return '' if filename is None else path.basename(filename)

    def get_oxidation_morphology_filename(self, obj):
        # 若没有附件，则返回''，否则返回文件名
        filename = obj.oxidation_morphology.name
        return '' if filename is None else path.basename(filename)


class ServicePropertiesSerializer(serializers.HyperlinkedModelSerializer):
    """
    服役性能管理序列器
    """
    readable_alloy_code = serializers.SerializerMethodField()

    class Meta:
        model = ServiceProperties
        fields = (
            'url', 'sample_name', 'alloy_code', 'data_source', 'sample_status',
            'equipment_name', 'equipment_manufacturer', 'device_model', 'encapsulation_way',
            'flux_type', 'temperature', 'cost_time', 'falling_life',
            'drop_impact_performance_curve', 'thermal_fatigue_cycle_interval', 'thermal_cycles',
            'thermal_fatigue_cycle_failure', 'ductile_brittle_shift_temperature',
            'readable_alloy_code'
        )

    def get_readable_alloy_code(self, obj):
        return obj.alloy_code.alloy_code
